<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>员工界面</title>
    <!--vue js-->
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <!--elementui css js-->
    <!-- 引入样式 -->
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    <!-- 引入组件库 -->
    <script src="https://unpkg.com/element-ui/lib/index.js"></script>
    <!--axios-->
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
    <style>

    </style>
</head>
<body>
<div id="app">
    <!--搜索-->
    <el-row>
        <el-col :span="6"><el-input v-model="ename" size="mini" placeholder="请输入员工姓名"></el-input></el-col>
<!--        <el-col :span="6"><el-input v-model="loc"  size="mini" placeholder="请输入"></el-input></el-col>-->
        <el-col :span="6">
            <el-select v-model="value4" size="mini" clearable placeholder="请选择所在部门">
                <el-option
                        v-for="item in options"
                        :key="item.deptNo"
                        :label="item.dname"
                        :value="item.deptNo">
                </el-option>
            </el-select>
        </el-col>
        <el-col :span="6"><el-button type="primary" @click="search"  size="mini" icon="el-icon-search">搜索</el-button>
            &nbsp;<el-button type="primary"  size="mini" icon="el-icon-circle-plus" @click="handleAdd">添加</el-button></el-col>
        <el-col :span="6"><div class="grid-content bg-purple-light">&nbsp;</div></el-col>
    </el-row>
    <!--分页列表显示-->
    <el-row>
        <el-table
                v-loading="loading2"
                element-loading-text="拼命加载中"
                element-loading-spinner="el-icon-loading"
                element-loading-background="rgba(0, 0, 0, 0.8)"
                :data="tableData"
                height="420"
                style="width: 100%">
            <el-table-column
                    prop="empNo"
                    label="员工编号"
                    width="180">
            </el-table-column>
            <el-table-column
                    prop="ename"
                    label="姓名"
                    width="180">
            </el-table-column>
            <el-table-column
                    prop="job"
                    label="职位">
            </el-table-column>
            <el-table-column
                    prop="mname"
                    label="上级">
            </el-table-column>
            <el-table-column
                    prop="hiredate"
                    label="入职时间">
            </el-table-column>
            <el-table-column
                    prop="sal"
                    label="薪资">
            </el-table-column>
            <el-table-column
                    prop="comm"
                    label="奖金">
            </el-table-column>
            <el-table-column
                    prop="dept.dname"
                    label="部门">
            </el-table-column>
            <el-table-column label="操作">
                <template slot-scope="scope">
                    <el-button
                            size="mini"
                            @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <el-button
                            size="mini"
                            type="danger"
                            @click="handleDelete(scope.$index, scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
    </el-row>
    <!--分页插件-->
    <el-row>
        <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="currentPage"
                :page-sizes="[2,3,5,10,100, 200, 300, 400]"
                :page-size="pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total">
        </el-pagination>
    </el-row><br>
    <!--百分比-->
    <el-row>
        <el-tooltip class="item" effect="dark" content="符合条件的员工占比" placement="right-start">
            <el-progress type="circle" :percentage="maths" v-bind:title="符合条件的员工占比" show-text="true"></el-progress>
        </el-tooltip>
    </el-row>
    <!--添加和更新 弹框-->
    <el-row>
        <el-dialog :title="title" :visible.sync="dialogFormVisible">
            <el-form :model="form">
                <el-form-item label="员工编号" :label-width="formLabelWidth" v-if="isShow">
                    <el-input v-model="form.empNo" auto-complete="off" :disabled="true"></el-input>
                </el-form-item>
                <el-form-item label="姓名" :label-width="formLabelWidth">
                    <el-input v-model="form.ename" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="职位" :label-width="formLabelWidth">
                    <el-input v-model="form.job" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="上级" :label-width="formLabelWidth">

                    <el-select v-model="form.mgr" clearable placeholder="请选择上级">
                        <el-option
                                v-for="item in options_mgr"
                                :key="item.empNo"
                                :label="item.ename"
                                :value="item.empNo">
                        </el-option>
                    </el-select>

                </el-form-item>
                <el-form-item label="入职时间" :label-width="formLabelWidth" v-if="isShow">
                    <el-input v-model="form.hiredate" auto-complete="off" :disabled="true"></el-input>
                </el-form-item>
                <el-form-item label="薪资" :label-width="formLabelWidth">
                    <el-input v-model="form.sal" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="奖金" :label-width="formLabelWidth">
                    <el-input v-model="form.comm" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="部门" :label-width="formLabelWidth">
                    <el-select v-model="form.deptNo" clearable placeholder="请选择所在部门">
                        <el-option
                                v-for="item in options"
                                :key="item.deptNo"
                                :label="item.dname"
                                :value="item.deptNo">
                        </el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="addOrUpdate">确 定</el-button>
            </div>
        </el-dialog>
    </el-row>
</div>
<script>
    var app = new Vue({
        el:'#app',
        data:{
            tableData:[],
            currentPage:1,//当前页面，默认1
            pageSize:2,//每页显示条数
            total:100,//分页总数量，默认值随便写，初始化时赋予新值
            ename:'',//查询功能
            dialogFormVisible:false,
            title:'员工添加',
            form:{},
            loading2:"true",  //加载提示
            options:[],    //部门下拉框
            options_mgr:[],  //上级下拉框
            value4:'',
            isShow:true,
            maths:0
        },
        created:function(){
            this.loading2 = "true";
            //初始化
            this.loadData();
            this.loadDept();
            this.loadMgr();
        },
        methods:{
            //计算百分比
            mathWay(){
                _this = this;
                axios.get('/emp/listCount')
                    .then(function (response) {
                        //正确请求时，回调函数
                        console.log("========="+response);
                        if(response.data.code==2000){
                            let max = response.data.data;
                            let min = _this.total;
                            _this.maths = min/max*100;
                        }else{
                            console.log("==========");
                        }
                    })
                    .catch(function (error) {
                        //失败处理
                        console.log(error);
                        console.log("==========");
                    });
            },

            //加载上级下拉框数据
            loadMgr(){
                _this = this;
                axios.get('/emp/list')
                    .then(function (response) {
                        //正确请求时，回调函数
                        console.log("========="+response);
                        if(response.data.code==2000){
                            _this.options_mgr = response.data.data;
                        }else{
                            console.log("==========");
                        }
                    })
                    .catch(function (error) {
                        //失败处理
                        console.log(error);
                        console.log("==========");
                    });
            },
            //加载部门下拉框数据
            loadDept(){
                _this = this;
                axios.get('/dept/list')
                    .then(function (response) {
                        //正确请求时，回调函数
                        console.log("========="+response);
                        if(response.data.code==2000){
                            _this.options = response.data.data;
                        }else{
                            console.log("==========");
                        }
                    })
                    .catch(function (error) {
                        //失败处理
                        console.log(error);
                        console.log("==========");
                    });
            },
            //加载员工数据
            loadData(){
                _this = this;
                //初始化
                axios.get('/emp/page',{
                    params: {
                        pageNo: this.currentPage,
                        pageSize:this.pageSize,
                        ename:this.ename,
                        deptNo:this.value4
                    }
                })
                    .then(function (response) {
                        //正确请求时，回调函数
                        console.log(response);
                        if(response.data.code==2000){
                            _this.tableData = response.data.data.pageEmp;
                            _this.total = response.data.data.total;
                            _this.loading2 = "";
                            _this.mathWay();
                        }
                    })
                    .catch(function (error) {
                        //失败处理
                        console.log(error);
                    });
            },
            //各种绑定事件方法
            //每页数量发生变化
            handleSizeChange(val) {
                console.log(`每页 ${val} 条`);
                this.pageSize=val;
                this.loadData();
                /* axios.get('/dept/page',{
                     params: {
                         pageNo: this.currentPage,
                         pageSize: this.pageSize
                     }
                 })
                     .then(function (response) {
                         //正确请求时，回调函数
                         console.log(response);
                         if(response.data.code==2000){
                             _this.tableData = response.data.data.pageDept;
                             _this.total = response.data.data.total;
                         }
                     })
                     .catch(function (error) {
                         //失败处理
                         console.log(error);
                     });*/
            },
            //当前页发生变化
            handleCurrentChange(val) {
                console.log(`当前页: ${val}`);
                this.currentPage=val;
                this.loadData();
                /*axios.get('/dept/page',{
                    params: {
                        pageNo: this.currentPage,
                        pageSize:this.pageSize
                    }
                })
                    .then(function (response) {
                        //正确请求时，回调函数
                        console.log(response);
                        if(response.data.code==2000){
                            _this.tableData = response.data.data.pageDept;
                            _this.total = response.data.data.total;
                        }
                    })
                    .catch(function (error) {
                        //失败处理
                        console.log(error);
                    });*/
            },
            //查询方法
            search(){
                this.loadData();
            },
            //处理添加
            handleAdd(){
                //入职时间隐藏
                this.isShow=false;
                //清空原来form
                this.form={};
                //更新显示名称
                this.title="员工添加";
                //对话框显示
                this.dialogFormVisible=true;
            },
            //处理编辑（弹出部门更新对话框）
            handleEdit(index, row) {
                console.log(index, row);
                //入职时间显示
                this.isShow=true;
                //{deptNo:10,dname:'ACCOUNTING',loc:'纽约'}
                this.form=row;
                //更新显示名称
                this.title="员工更新";
                //对话框显示
                this.dialogFormVisible=true;
            },

            //提交表单数据（添加或者更新）
            addOrUpdate(){
                var  url ='/emp/update';
                if(this.form.empNo==''||this.form.empNo==null){
                    url="/emp/add"
                }
                _this=this;
                //提交方法
                axios.post(url, this.form)
                    .then(function (response) {
                        console.log(response);
                        //提交成功
                        if(response.data.code==2000){
                            //关闭弹出框
                            _this.dialogFormVisible=false;
                            //提示更新成功
                            _this.$message({
                                //  message: '恭喜你，这是一条成功消息',
                                message: response.data.message,
                                type: 'success'
                            });
                            //重新加载数据
                            _this.loadData();
                        }else {
                            _this.$message.error(response.data.message);
                        }
                    })
                    .catch(function (error) {
                        _this.$message.error('错了哦，这是一条错误消息');
                    });
            },
            //处理删除（ 部门删除）
            handleDelete(index, row) {
                console.log(index, row);
                _this=this;
                //确认弹框
                this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    //执行删除
                    axios.delete('/emp/delete?empNo='+row.empNo)
                        .then(function (response) {
                            //判断删除是否成功
                            if(response.data.code==2000){
                                //提示
                                _this.$message({
                                    type: 'success',
                                    message: '删除成功!'
                                });
                                //重新加载数据
                                _this.loadData();
                            }else {
                                _this.$message.error(response.data.message);
                            }
                        })
                        .catch(function (error) {
                            //失败处理
                            console.log(error);
                            _this.$message.error('删除失败!');
                        });
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });


            }
        }
    });
</script>
</body>
</html>